package com.itheima.Interceptor;

import com.itheima.common.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Component
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("拦截器{}", handler);
        String token = request.getHeader("token");
        try {
            JwtUtils.parseJWT(token);
        }
        catch (Exception e){
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
